const { Schema } = require('mongoose')
const mongoose = require('./db')
const { everNickname } = require('../utils/methodsSet/index')
// 测式模型
const userSchema = mongoose.Schema({
    userId: Number,//数据类型
    userName: String,
    age: Number,
    msg: {
        type: String,
        default: 'hello koa2+mongodb'//配置默认
    }
})

//用户模型
const UserSchema = mongoose.Schema({
    username: {
        type: String,
        require: true,
        // maxlength:[16,'用户名长度不大于16'],
        // minlength:[3,'用户名长度不小于3'],
        trim: true,
    },//用户名
    password: String,//密码。
    userinfo: {
        type: Schema.Types.ObjectId,
        ref: "userinfo",
        require: false
    },
})

//用户详细模型
const UserInfoSchema = mongoose.Schema({
    nickname: {
        type: String,
        default: everNickname(),
        // maxlength:[12,'昵称长度不大于12'],
        // minlength:[2,'昵称长度不小于2'],
        trim: true,
    }, //Ro酱
    email: {
        type: String,
        default: "",
    },   //3399609750@qq.com
    realname: {
        type: String,
        default: "",
        // maxlength:[5,'真实姓名长度不大于5'],
        // minlength:[2,'真实姓名长度不小于2'],
    },  //潘子庆
    telephone: {
        type: String,
        default: "",
        trim: true,
    },// 0756-123456
    mobile: {
        type: String,
        default: "",
        trim: true,
        // maxlength:[11,'手机号码长度不大于11'],
        // minlength:[11,'真实姓名长度不大于11'],
    },//19912075710
    avantar: {
        type: String,
        default: "https://img1.baidu.com/it/u=4089853289,2504866502&fm=253&fmt=auto&app=138&f=PNG?w=500&h=583",//默认头像
        trim: true,
    },//头像
    department: {
        type: String,
        ref: "department",
        default: "",
        trim: true,
    },//维修部门
    role: {
        type: String,
        ref: "role",
        default: "",
        trim: true,
    },//电力工程师

    // 随意添加的字段
    fieldPeople: {
        type: String,
        default: "",
        require: false
    }

})

//部门模型
const departmentSchema = mongoose.Schema({
    dname: String,
    desc: String
})
//角色模型
const roleSchema = mongoose.Schema({
    rname: String,
    rdesc: String,
})


// 消息中心通知
const MessageSchema = mongoose.Schema({
    title: {type:String,trim:true},//消息标题
    createTime: {//创建时间
        type: Number,
        default: new Date().getTime(),
    },
    state: {//消息状态，已读未读。
        type: Number,
        default: 0,
    },
    order:{
        type:Schema.Types.ObjectId,
        ref:"state"
    }
})

const MailListSchema=mongoose.Schema({
    name:String,
    img:{
        type:String,
        default:""
    },
    info:{
        type:Schema.Types.ObjectId,
        ref:"userinfo"
    }
})

const UserModel = mongoose.model("user", UserSchema)//用户登录
const UserInfoModel = mongoose.model("userinfo", UserInfoSchema)//用户详细
const departmentModel = mongoose.model("department", departmentSchema)//部门
const roleModel = mongoose.model("role", roleSchema)//角色
const MessageModel = mongoose.model("message", MessageSchema)//消息
const MailListModel=mongoose.model("maillist",MailListSchema)

module.exports = {
    UserModel,
    UserInfoModel,
    departmentModel,
    roleModel,
    MessageModel,
    MailListModel,
}
